package mediatheque.view;

import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import mediatheque.model.MediathequeModel;
import mediatheque.model.PretModel;

public class PretView extends JDialog {

	private static final long serialVersionUID = 316861042021923944L;

	protected MediathequeModel model;
	
    protected JButton btnAnnuler;
    protected JButton btnEnregistrer;
    protected JButton btnRetour;

    protected JLabel lblTitre;
    protected JLabel lblEmprunteur;
    protected JLabel lblDocument;    

    protected JTextField tfEmprunteur;  
    protected JTextField tfDocument; 

    private JButton btnChercherEmprunteur;
    private JButton btnChercherDocument;
	
	
	public PretView(MediathequeModel model, JFrame parent) {
		super(parent, "Gestion d'un pret", true);
		this.model = model;	

		setSize(440, 210);
		setLocationRelativeTo(null);
		setResizable(false);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		
		initialiserComposants();		
	}

	protected void initialiserComposants() {
				
		Font font = new java.awt.Font("Verdana", 0, 12);
		
		lblTitre = new JLabel("Edition d'un pret");
		lblTitre.setFont(font);
		lblTitre.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        lblEmprunteur = new JLabel("Emprunteur : ");
        tfEmprunteur = new JTextField();
        tfEmprunteur.setEditable(false);

        lblDocument = new JLabel("Document :");
        tfDocument = new JTextField();
        tfDocument.setEditable(false);
 
        
        btnChercherEmprunteur = new JButton("Chercher");
        btnChercherDocument = new JButton("Chercher");

        btnEnregistrer = new JButton("Enregistrer");
        btnEnregistrer.setFont(font);  
        btnRetour = new JButton("Retour");
        btnRetour.setFont(font); 
		btnRetour.setEnabled(false);       
        btnAnnuler = new JButton("Annuler");
        btnAnnuler.setFont(font);
        
        
        placerComposants();
	}

	protected void placerComposants() {
		GroupLayout layout = new GroupLayout(this.getContentPane());
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(lblTitre, GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(lblEmprunteur, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDocument, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(tfEmprunteur, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnChercherEmprunteur))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                    .addComponent(tfDocument, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnRetour, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnEnregistrer)))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnAnnuler, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnChercherDocument, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitre, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEmprunteur, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnChercherEmprunteur)
                    .addComponent(tfEmprunteur, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(btnChercherDocument)
                    .addComponent(lblDocument, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDocument, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAnnuler)
                    .addComponent(btnEnregistrer)
                    .addComponent(btnRetour))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
	}

	public void addChercherEmprunteur(ActionListener al){
		btnChercherEmprunteur.addActionListener(al);
	}
	
	public void addChercherDocument(ActionListener al){
		btnChercherDocument.addActionListener(al);
	}
	
	public void addRetournerListener(ActionListener al){
		btnRetour.addActionListener(al);
	}

	public void addEnregistrerListener(ActionListener al){
		btnEnregistrer.addActionListener(al);
	}
	
	public void addAnnulerListener(ActionListener al){
		btnAnnuler.addActionListener(al);
	}

	public String getValeurEmprunteur(){
		return tfEmprunteur.getText();
	}
	
	public String getValeurDocuments(){
		return tfDocument.getText();
	}
	

	public void setValeurs(PretModel p) {
		btnRetour.setEnabled(true);
		setValeurEmprunteur(p.getIdentifiantEmprunteur());
		setValeurDocument(p.getIdentifiantDocument());
	}

	public void setValeurDocument(String identifiantDocument) {
		tfDocument.setText(identifiantDocument);
	}

	public void setValeurEmprunteur(String identifiantEmprunteur) {
		tfEmprunteur.setText(identifiantEmprunteur);	
	}
	
}
